package com.lijian.controller;

import com.lijian.base.PageTableRequest;
import com.lijian.base.Result;
import com.lijian.dto.UserDto;
import com.lijian.model.SysUser;
import com.lijian.service.UserService;
import com.lijian.util.MD5;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询用户信息", notes = "分页查询用户信息")
    @ApiImplicitParam(name = "request", value = "分页查询用户信息", required = false)
    @PreAuthorize("hasAnyAuthority('sys:user:query')")
    @ResponseBody
    public Result<SysUser> findUsers(PageTableRequest request) {
        request.countOffset();
        return userService.findAllUsersByPage(request.getOffset(), request.getLimit());
    }

    @GetMapping("/add")
    @PreAuthorize("hasAnyAuthority('sys:user:add')")
    @ApiOperation(value = "用户新增页面", notes = "跳转到新增用户信息页面")
    public String addUser(Model model) {
        model.addAttribute(new SysUser());
        return "user/user-add";
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('sys:user:add')")
    @ApiOperation(value = "保存用户信息", notes = "保存新增的用户信息")//描述
    @ResponseBody
    public Result<SysUser> saveUser(SysUser sysUser, Integer roleId) {
        return userService.save(sysUser, roleId);
    }

    /**
     * yyyy-MM-dd字符串自动转 Date
     *
     * @param binder
     * @param request
     */
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @GetMapping("/edit")
    @ApiOperation(value = "用户编辑页面", notes = "跳转到用户信息编辑页面")//描述
    @ApiImplicitParam(name = "user", value = "用户实体类", dataType = "SysUser")
    @PreAuthorize("hasAnyAuthority('sys:user:edit')")
    public String updateUser(Model model, SysUser sysUser) {
        //查出来页面回写
        model.addAttribute(userService.findUserById(sysUser.getId()));
        return "user/user-edit";
    }

    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys:user:edit')")
    @ApiOperation(value = "保存用户信息", notes = "保存编辑完的用户信息")//描述
    @ResponseBody
    public Result<SysUser> updateUser(SysUser sysUser, Integer roleId) {
        return userService.update(sysUser, roleId);
    }

    @GetMapping("/delete")
    @PreAuthorize("hasAnyAuthority('sys:user:del')")
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")//描述
    @ApiImplicitParam(name = "id", value = "id", required = true)
    @ResponseBody
    public Result deleteUser(Integer id) {
        return userService.deleteById(id);
    }

    @GetMapping("/findUserBySearch")
    @PreAuthorize("hasAuthority('sys:user:query')")
    @ApiOperation(value = "模糊查询用户信息", notes = "模糊搜索查询用户信息")//描述
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "模糊搜索的用户名", required = true),
    })
    @ResponseBody
    public Result<SysUser> findUserBySearch(PageTableRequest request, String username) {
        request.countOffset();
        return userService.findUserBySearch(username, request.getOffset(), request.getLimit());
    }

    /**
     * 根据复选框批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteUsersByCheck")
    @PreAuthorize("hasAnyAuthority('sys:user:del')")
    @ApiOperation(value = "批量删除用户信息", notes = "批量删除用户信息")//描述
    @ApiImplicitParam(name = "ids", value = "ids", required = true)
    @ResponseBody
    public Result deleteUsersByCheck(@RequestParam String ids) {
        return userService.deleteUsersByCheck(ids);
    }

    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ResponseBody
    public Result<SysUser> changePassword(@RequestParam String username, @RequestParam String oldPassword, @RequestParam String newPassword) {
        return userService.changePassword(username, oldPassword, newPassword);
    }
}
